package com.ijava.base.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import com.ijava.base.domain.User;
import com.ijava.base.service.UserService;
import com.ijava.base.dao.UserDAO;

public class UserServiceImpl implements UserService {
	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	private UserDAO dao;

	// private DataSource dataSource;

	// public DataSource getDataSource() {
	// return dataSource;
	// }
	//
	// public void setDataSource(DataSource dataSource) {
	// this.dataSource = dataSource;
	// }

	public UserDAO getDao() {
		return dao;
	}

	public void setDao(UserDAO dao) {
		this.dao = dao;
	}

	public void remove(String id) throws Exception {
		dao.remove(id);
	}

	public void mutilRemove(User user) throws Exception {
		try {
			String[] keys = user.getKeys();
			if(null == keys)
				return;
			for (int i = 0; i < keys.length; i++) {
				remove(keys[i]);
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

	public void save(User user) throws Exception {
		dao.save(user);
	}

	public void update(User user) throws Exception {
		dao.update(user);
	}

	public void saveOrUpdate(User user) throws Exception {
		dao.saveOrUpdate(user);
	}

	public User get(String id) throws Exception {
		return dao.get(id);
	}

	public List findWithPaging(User user) throws Exception {
		try {
			int firstRow = (user.getPageNo() - 1) * user.getPageSize();
			int maxRow = user.getPageSize();

			// JdbcTemplate template = new JdbcTemplate(dataSource);
			// int total =
			// template.queryForInt("SELECT COUNT(1) FROM TABLE_NAME");
			// user.setTotal(total);

			List paramList = new ArrayList();
			StringBuffer condition = new StringBuffer(" where 1 = 1 ");
			if (StringUtils.hasLength(user.getLoginId())) {
				condition.append(" and loginId like ?");
				paramList.add("%" + user.getLoginId() + "%");
			}
			if (StringUtils.hasLength(user.getRealName())) {
				condition.append(" and realName like ?");
				paramList.add("%" + user.getRealName() + "%");
			}
			if (StringUtils.hasLength(user.getEmail())) {
				condition.append(" and email like ?");
				paramList.add("%" + user.getEmail() + "%");
			}
			if (StringUtils.hasLength(user.getMobile())) {
				condition.append(" and mobile like ?");
				paramList.add("%" + user.getMobile() + "%");
			}
			if (StringUtils.hasLength(user.getPhone())) {
				condition.append(" and phone like ?");
				paramList.add("%" + user.getPhone() + "%");
			}
			if (StringUtils.hasLength(user.getFax())) {
				condition.append(" and fax like ?");
				paramList.add("%" + user.getFax() + "%");
			}

			int total = dao.find(
					"select count(*) from " + User.class.getName()
							+ condition.toString(), paramList).size();
			user.setTotal(total);

			String queryString = "from " + User.class.getName()
					+ condition.toString();
			return dao.findWithPaging(queryString, paramList, firstRow, maxRow);

		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
	}

}
